<template>
  <el-drawer
    title="同步视频"
    :visible="visible"
    direction="rtl"
    size="40%"
    :wrapper-closable="false"
    :before-close="handlerClose"
  >
    <div class="drawer-content">
      <el-form ref="category" :model="formData" size="small" inline>
        <el-form-item label="服务商">
          <el-radio-group v-model="formData.stock" size="medium">
            <el-radio-button label="aliyun">阿里云-云点播</el-radio-button>
            <el-radio-button label="bokecc">bokecc-云点播</el-radio-button>
            <el-radio-button label="tencent">腾讯云-云点播</el-radio-button>
          </el-radio-group>
        </el-form-item>
        <el-form-item v-if="formData.stock !== 'tencent'" label="">
          <div>
            <p>
              <span> 按每页 </span>
              <el-input-number v-model="formData.page_size" :min="10" :step="1"></el-input-number>
              <span> 条视频，同步从 </span>
              <el-input-number v-model="formData.start_page" :min="1" :step="1"></el-input-number>
              <span> 页到 </span>
              <el-input-number v-model="formData.end_page" :min="0" :max="100" :step="1"></el-input-number>
              <span> 页的视频。 </span>
            </p>
            <div class="edu-tips">结束页值为 0 时，表示同步从起始页之后的所有视频</div>
            <div class="tips-row">
              <div class="edu-tips">1、同步过程中，请勿更改【上传-云端储存服务商】配置；</div>
              <div class="edu-tips">2、同步任务将在后台执行，同步结果在【系统消息】中通知。</div>
            </div>
          </div>
        </el-form-item>
        <el-form-item v-else label="上传文件">
          <el-upload
            action=""
            :http-request="fileUpload"
            :limit="1"
            :before-upload="beforeUpload"
            :on-success="fileOnSuccess"
            :on-remove="handlerRemove"
            accept=".jsonl"
          >
            <el-button type="primary">点击上传</el-button>
            <div slot="tip" class="el-upload__tip">
              <div class="edu-tips">1、上传从腾讯控制台中导出的JSONLINES文件，一次只能上传一个；</div>
              <div class="edu-tips">2、单个文件不得超过100M，且不得超过5000条数据。</div>
            </div>
          </el-upload>
        </el-form-item>
      </el-form>
      <form-footer
        class="dialog-footer"
        :loading="disableSubmit"
        confirm-btn-text="开始同步"
        @cancel="handlerClose"
        @submit="handlerConfirm"
      />
    </div>
  </el-drawer>
</template>

<script>
import FormFooter from '@/components/form-footer'

export default {
  name: 'SyncVideoDialog',
  components: {
    FormFooter
  },
  props: {
    visible: {
      type: Boolean,
      require: true
    }
  },
  data() {
    return {
      disableSubmit: false,
      formData: {
        stock: 'aliyun',
        start_page: 1,
        end_page: 0,
        page_size: 10,
        attach_id: 0
      }
    }
  },
  methods: {
    // 关闭弹出层
    handlerClose() {
      this.$emit('update:visible', false);
    },
    // 同步视频
    handlerConfirm() {
      // 检测参数
      if (this.formData.end_page !== 0 && this.formData.end_page < this.formData.start_page) {
        this.$message.warning('结束页不能小于开始页')
        return
      }

      if (this.formData.stock === 'tencent' && !this.formData.attach_id) {
        this.$message.warning('请上传文件')
        return
      }

      const txt = this.formData.stock === 'tencent'
          ? '该操作将读取文件中的视频数据同步到本地，确定执行吗？'
          : '该操作将云端视频同步到本地，云端无分类的视频将存入默认分类，确定执行吗？'
      this.$box
          .confirm(txt)
          .then(() => {
            this.dialogVisible = false;
            this.$http.post(
                '/admin/material/sync/video/local',
                {
                  ...this.formData
                },
                res => {
                  this.$message.success(res.msg)
                  // this.reload();
                  this.handlerClose()
                },
                err => {
                  this.$message.error(err.msg);
                },
                false
            );
          })
          .catch(() => {});
    },
    /** 自定义文件上传方法,采用分片上传 */
    fileUpload: function (params) {
      // 指定为临时文件
      params.is_temp = true
      // 分片上传 每2M为一片
      this.$upload.submit(params, true, 2);
    },
    beforeUpload(file) {
      // // 文件类型是否符合
      // const isDocx = file.type == 'application/vnd.openxmlformats-officedocument.wordprocessingml.document';
      //
      // if (!isDocx) {
      //   this.$message.error('请上传docx格式的模板');
      //   return false;
      // }

      // 检测大小
      const isLt100M = file.size / 1024 / 1024 <= 100;
      if (!isLt100M) {
        this.$message.error('最大只能上传100M的文件');
        return false;
      }

      return true;
    },
    fileOnSuccess(response, file) {
      this.formData.attach_id = file.id || response.data.id
    },
    handlerRemove() {
      this.formData.attach_id = 0
    }
  }
}

</script>

<style scoped lang="scss">
.drawer-content{
  padding: 0 20px 20px 20px;
  .edu-tips{
    .el-link{
      vertical-align: inherit;
    }
  }
  .tips-row{
    margin-top: 20px;
  }
}
</style>
